<?php
App::import('Model', 'Movimientostubo');
$modeloMovimientosTubo = new Movimientostubo();

App::import('Model', 'Cortestubo');
$modeloCortesTubo = new Cortestubo();

$rolUsuario = $this->Session->read('Auth.User.role');
$sucursalUsuario = $this->Session->read('Auth.User.sucursale_id');
$categoria = $listadoTubos['0']['Tubo']['categoria_id'];
?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#grid').dataTable({
            "bJQueryUI": false,
            "bAutoWidth": false,
            "sPaginationType": "full_numbers",
            "sDom": '<"H"fl>t<"F"ip>',
            "aaSorting": [[0, "desc"]]
        });
    });
</script>
<?php foreach ($listadoTubos as $t): ?>
    <style>
        #element_to_pop_up_<?php echo $t['Tubo']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none; 
            padding:5px;
            min-width:300px;
            min-height: 180px;        
        }

        #element_to_pop_up_quita_<?php echo $t['Tubo']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none; 
            padding:5px;
            width:320px;
            min-height: 180px;        
        }

        .b-close{
            cursor:pointer;
            position:absolute;
            right:10px;
            top:5px;            
        }  

        /*        regulariza los cortes*/
        #element_to_pop_up_regulariza_<?php echo $t['Tubo']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none; 
            padding:5px;
            min-width:300px;
            min-height: 180px;        
        }
    </style>
<?php endforeach; ?>

<?php
$verificaCodigo = $listadoTubos['0']['Tubo']['categoria_id'];
switch ($idCategoria) {
    case 18:
        $nombreCategoria = 'LIQUIDO';
        $sacar = 'SACAR LIQUIDO';
        $ver = 'VER LIQUIDOS PADRES';
        $vender = 'VENDER LIQUIDO';
        $ed = 'EDITAR DIVISION';
        $dividir = 'DIVIDIR LIQUIDO';
        $ingreso = 'INGRESAR LIQUIDO';
        $cortes = 'PADRES DEL LIQUIDO';
        break;
    case 19:
        $nombreCategoria = 'BOBINA';
        $sacar = 'SACAR BOBINA';
        $ver = 'VER BOBINAS PADRES';
        $vender = 'VENDER BOBINA';
        $ed = 'EDITAR CORTE';
        $dividir = 'DIVIDIR BOBINA';
        $ingreso = 'INGRESAR BOBINA';
        $cortes = 'PADRES DE LA BOBINA';
        break;
    case 20:
        $nombreCategoria = 'PURPURINA';
        $sacar = 'SACAR PURPURINA';
        $ver = 'VER PURPURINAS PADRES';
        $vender = 'VENDER PURPURINA';
        $ed = 'EDITAR DIVISION';
        $dividir = 'DIVIDIR PURPURINA';
        $ingreso = 'INGRESAR PURPURINA';
        $cortes = 'PADRES DE LA PURPURINA';
        break;
}
//debug($nombreCategoria);exit;
?>
<?php foreach ($listadoTubos as $t): ?>
    <div id="element_to_pop_up_<?php echo $t['Tubo']['id']; ?>">
        <a class="b-close">X</a>                    
        <div class="widget grid12">
            <div class="whead"><h6>INGRESO AL ALMACEN </h6><div class="clear"></div></div>
            &nbsp;
            <h4>&nbsp;&nbsp; PRODUCTO: <?php echo $t['Tubo']['nombre']; ?></h4>
            <?php echo $this->Form->create('Movimientostubos', array('action' => 'ingresoalmacen', 'id' => "validate")); ?>
            <?php echo $this->Form->hidden('categoria', array('value' => $t['Tubo']['categoria_id'])); ?>
            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Cantidad:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php //echo $this->Form->text('cantidad', array('size' => 3, 'required', 'style' => 'width: 60px;'));  ?>
                    <input type="number" name="data[Movimientostubos][cantidad]" min="1" required />
                    <?php echo $this->Form->hidden('producto_id', array('value' => $t['Tubo']['id'])); ?>
                </div>
            </div> 

            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Prestado:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php //echo $this->Form->text('cantidad', array('size' => 3, 'required'));  ?>                    
                    <input type="checkbox" value="S" name="data[Movimientostubos][prestado]" onclick="muestraquien_<?php echo $t['Tubo']['id']; ?>()" />
                </div>
            </div>

            <div class="formRow" id="mostrarquien<?php echo $t['Tubo']['id']; ?>" style="display: none;">                                                                                                                
                <div class="grid3"><label>Quien Presto:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php echo $this->Form->text('quienpresto', array('size' => 3, 'style' => 'width: 150px;')); ?>                    
                </div>
            </div>              

            <div class="clear"></div>
            <?php if ($rolUsuario == 'admin'): ?>
                <div class="formRow">
                    <input type="submit" value="Ingresar Producto" class="buttonS bGreen" /><div class="clear"></div>            
                </div>
            <?php else: ?>
            <?php endif; ?>             
            </form>
            <script>
    function muestraquien_<?php echo $t['Tubo']['id']; ?>() {
        //console.log('activo');
        $("#mostrarquien<?php echo $t['Tubo']['id']; ?>").toggle('slow', function() {
            //console.log('Mostro Quien');
            //alert('mostrar');
        });
    }
            </script>    
        </div>
    </div> 

    <div id="element_to_pop_up_quita_<?php echo $t['Tubo']['id']; ?>">
        <a class="b-close">X</a>                    
        <div class="widget grid12">
            <div class="whead"><h6>EDITA CANTIDAD </h6><div class="clear"></div></div>
            &nbsp;
            <h4>&nbsp;&nbsp; PRODUCTO: <?php echo $t['Tubo']['nombre']; ?></h4>
            <h6 style="color: #f00;">&nbsp;&nbsp;Precaucion al editar este dato puede causar incoherencia en los datos</h6>
            <?php echo $this->Form->create('Movimientostubos', array('action' => 'quita', 'id' => "validate")); ?>

            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Cantidad:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php echo $this->Form->text('cantidad', array('size' => 3, 'required', 'style' => 'width: 60px;')); ?>
                    <?php echo $this->Form->hidden('tubo_id', array('value' => $t['Tubo']['id'])); ?>
                </div>
            </div>             

            <div class="clear"></div>
            <?php if ($rolUsuario == 'admin'): ?>
                <div class="formRow">
                    <input type="submit" value="Regularizar Producto" class="buttonS bGreen" /><div class="clear"></div>            
                </div>
            <?php else: ?>
            <?php endif; ?>

            </form>         
        </div>
    </div> 

    <div id="element_to_pop_up_regulariza_<?php echo $t['Tubo']['id']; ?>">
        <?php $codActualiza = $t['Tubo']['id']; ?>
        <a class="b-close">X</a>                    
        <div class="widget grid12">
            <div class="whead"><h6>REGULARIZACION</h6><div class="clear"></div></div>
            &nbsp;
            <h4>&nbsp;&nbsp; PRODUCTO: <?php echo $t['Tubo']['nombre']; ?></h4>
            <?php echo $this->Form->create('Cortestubos', array('action' => 'ajaxregulariza')); ?>
            <?php echo $this->Form->hidden('categoria', array('value' => $categoria)); ?>
            <?php
            /* echo $this->Ajax->form(array('type' => 'post',
              'options' => array(
              'model' => 'Cortestubos',
              'update' => "actualiza$codActualiza",
              'url' => array(
              'controller' => 'Cortestubos',
              'action' => 'ajaxregulariza'
              ),
              )
              )); */
            ?>
            <div id="actualiza<?php echo $codActualiza; ?>">   
            </div> 
            <?php if ($categoria == 19): ?>
                <div class="formRow">                                                                                                                
                    <div class="grid3"><label>Alto (Centimetros): <span class="req">*</span></label></div>
                    <div class="grid3">
                        <?php //echo $this->Form->text('alto', array('size' => 3, 'required', 'style' => 'width: 80px;'));  ?>                      
                        <input type="number" name="data[Cortestubos][alto]" min="1" required style="width: 90px;" step="any" />
                    </div>
                </div>    
            <?php else: ?>
            <?php endif; ?>
            <?php echo $this->Form->hidden('tubo_id', array('value' => $t['Tubo']['id'])); ?>  
            <div class="clear"></div>
            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Peso (Kilos): <span class="req">*</span></label></div>
                <div class="grid3">
                    <?php //echo $this->Form->text('peso', array('size' => 3, 'required', 'style' => 'width: 80px;'));  ?>    
                    <input type="number" name="data[Cortestubos][peso]" min="1" required style="width: 90px;" step="any" />
                </div>
            </div>    
            <div class="clear"></div>
            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Prestado:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php //echo $this->Form->text('cantidad', array('size' => 3, 'required'));  ?>                    
                    <input type="checkbox" value="S" name="data[Cortestubos][prestado]" onclick="regMuestraquien_<?php echo $t['Tubo']['id']; ?>()">
                </div>
            </div>

            <div class="formRow" id="regmostrarquien<?php echo $t['Tubo']['id']; ?>" style="display: none">                                                                                                                
                <div class="grid3"><label>Quien Presto:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php echo $this->Form->text('quienpresto', array('size' => 3, 'style' => 'width: 150px;')); ?>                    
                </div>
            </div> 

            <script>
    function regMuestraquien_<?php echo $t['Tubo']['id']; ?>() {
        //console.log('activo');
        $("#regmostrarquien<?php echo $t['Tubo']['id']; ?>").toggle('slow', function() {
            //console.log('Mostro Quien');
            //alert('mostrar');
        });
    }
            </script>
            <div class="formRow">
                <input type="submit" value="Regulariza Producto" class="buttonS bGreen" /><div class="clear"></div>            
            </div>
            </form>
        </div>
    </div>
<?php endforeach; ?>  
<div class="widget grid4">
    <div class="whead"><h6>BUSQUEDA DE <?php echo $nombreCategoria; ?></h6><div class="clear"></div></div>
    <?php //echo $this->Form->create('Tubo', array('id' => "validate"));  ?>
    <?php
    echo
    $this->Ajax->form(array('type' => 'post',
        'options' => array(
            'model' => 'Tubo',
            'update' => 'mustraResultados',
            'url' => array(
                'controller' => 'Tubos',
                'action' => 'ajaxbusca'
            ),
        )
    ));
    ?>
    <div class="formRow">
        <div class="formRow">
            <div class="grid3"><label>Codigo:<span class="req">*</span></label></div>
            <div class="grid3">
                <?php echo $this->Form->text('codigo', array('class' => 'validate[required] input-text', 'size' => '40', 'id' => 'validate')); ?>
                <?php echo $this->Form->hidden('categotria', array('value' => $categoria)); ?>
            </div> 

            <div class="grid3"><label>&nbsp;</label></div>

            <div class="grid3">
                <input type="submit" value="BUSCAR CODIGO" class="buttonM bGreen formSubmit"><div class="clear"></div>            
            </div> 

        </div>                   
        </form>
    </div>
</div>

<div class="widget grid6">
    <div class="whead"><h6>RESULTADOS DE LA BUSQUEDA</h6><div class="clear"></div></div>
    <div class="shownpars">
        <div id="mustraResultados">

        </div>                   
    </div>
</div>


<div class="widget grid12">        

    <div class="whead">
        <h6>
            LISTADO DE 
            <?php echo $nombreCategoria . 'S'; ?>
        </h6>
        <div class="clear"></div></div>
    <div class="shownpars">
        <table cellpadding="0" cellspacing="0" border="0" id="grid">
            <thead>
                <tr>                    
                    <th>Nombre</th>
                    <?php if ($listadoTubos['0']['Tubo']['categoria_id'] == 19): ?>
                        <th>Micraje</th>                                        
                        <th>Alto</th>
                    <?php else: ?>
                    <?php endif; ?>
                    <th>Peso</th>
                    <th>Cantidad (Enteros)</th>
                    <th>Cantidad (Medios)</th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php //$i = 1;   ?>                
                <?php foreach ($listadoTubos as $t): ?>                                
                    <tr>                
                        <td><?php echo $t['Tubo']['nombre']; ?></td> 
                        <?php if ($listadoTubos['0']['Tubo']['categoria_id'] == 19): ?>
                            <td><?php echo $t['Tubo']['micraje']; ?></td>
                            <td><?php echo $t['Tubo']['alto']; ?> Cm</td>
                        <?php else: ?>
                        <?php endif; ?>
                        <td><?php echo $t['Tubo']['peso']; ?> Kilos</td> 
                        <td>
                            <?php
                            if ($sucursalUsuario == 6) {
                                $cantidadAlmacen = $modeloMovimientosTubo->find('first', array(
                                    'recursive' => -1,
                                    'conditions' => array(
                                        'Movimientostubo.tubo_id' => $t['Tubo']['id']
                                    ),
                                    'order' => 'Movimientostubo.id DESC'
                                ));
                                //debug($cantidadAlmacen);
                                if (empty($cantidadAlmacen)) {
                                    echo '0';
                                } else {
                                    echo $cantidadAlmacen['Movimientostubo']['cantidad'];
                                }
                            } else {
                                $cantidadAlmacen = $modeloMovimientosTubo->find('count', array(
                                    'conditions' => array(
                                        'Movimientostubo.sucursale_id' => $sucursalUsuario
                                    )
                                ));
                                //echo $cantidadAlmacen;
                                if ($cantidadAlmacen == 0) {
                                    echo '0';
                                } else {
                                    echo $cantidadAlmacen['Movimientostubo']['cantidad'];
                                }
                            }
                            ?>
                            unidades
                        </td>
                        <td>
                            <?php
                            $cantidadMitades = $modeloCortesTubo->find('count', array(
                                'conditions' => array(
                                    'Cortestubo.sucursale_id' => $sucursalUsuario,
                                    'Cortestubo.tubo_id' => $t['Tubo']['id'],
                                    'Cortestubo.estado' => 'C'
                                )
                            ));
                            //debug($cantidadMitades);
                            echo $cantidadMitades;
                            ?>
                            unidades
                        </td>
                        <td>          
                            <?php if ($sucursalUsuario == 6): ?>
                                <a onclick="ajaxIngreso<?php echo $t['Tubo']['id']; ?>();"><?php echo $this->Html->image("iconos/add.png", array('title' => 'Ingreso Almacen')) ?></a>&nbsp;&nbsp;
                            <?php else: ?>
                            <?php endif; ?>
                            <a onclick="ajaxRegulariza<?php echo $t['Tubo']['id']; ?>();"><?php echo $this->Html->image("iconos/regulariza.png", array('title' => 'Ingresar Sobrantes')) ?></a>&nbsp;&nbsp;                        
                            <a href="<?php echo $this->Html->url(array('controller' => 'Tubos', 'action' => 'verdetalle', $t['Tubo']['id'])); ?>"><?php echo $this->Html->image('iconos/listaInv.png', array('title' => 'Listado de Productos')) ?></a>&nbsp;&nbsp;                                                                                                          
                            <?php
                            echo $this->Form->postLink(
                                    $this->Html->image('iconos/elim.png', array(
                                        "title" => 'Eliminar Producto'
                                    )), array(
                                'action' => 'eliminar',
                                $t['Tubo']['id']), array(
                                'escape' => false,
                                'confirm' => 'Estas Seguro?')
                            );
                            ?>
                            <a onclick="ajaxQuita<?php echo $t['Tubo']['id']; ?>();"><?php echo $this->Html->image("iconos/alert.png", array('title' => 'Regulariza')) ?></a>&nbsp;&nbsp;
                        </td>
                    </tr>
                        <?php endforeach; ?>               
            </tbody>
        </table>
                        <?php foreach ($listadoTubos as $t): ?>
            <script>
    function ajaxIngreso<?php echo $t['Tubo']['id']; ?>() {
        // Triggering bPopup when click event is fired
        $('#element_to_pop_up_<?php echo $t['Tubo']['id']; ?>').bPopup({
            fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
            followSpeed: 1500, //can be a string ('slow'/'fast') or int                 
            //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                   ?>').bPopup()
        });
    }

    function ajaxQuita<?php echo $t['Tubo']['id']; ?>() {
        //console.log('CLick');
        // Triggering bPopup when click event is fired
        $('#element_to_pop_up_quita_<?php echo $t['Tubo']['id']; ?>').bPopup({
            fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
            followSpeed: 1500, //can be a string ('slow'/'fast') or int                 
            //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                    ?>').bPopup()
        });
    }

    function ajaxRegulariza<?php echo $t['Tubo']['id']; ?>() {
        // Triggering bPopup when click event is fired
        $('#element_to_pop_up_regulariza_<?php echo $t['Tubo']['id']; ?>').bPopup({
            fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
            followSpeed: 1500, //can be a string ('slow'/'fast') or int                
            //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                    ?>').bPopup()
        });
    }
            </script>
<?php endforeach; ?>
<?php if ($rolUsuario == 'admin'): ?>
    <?php
    switch ($idCategoria) {
        case 19:
            echo $this->Html->link('INSERTAR NUEVO PRODUCTO', array('action' =>
                'insertar'), array('class' => 'buttonS bGreen'));
            break;
        case 18:
            echo $this->Html->link('INSERTAR NUEVO PRODUCTO', array('controller' => 'Valdes', 'action' =>
                'insertar'), array('class' => 'buttonS bGreen'));
            break;
        case 20:
            echo $this->Html->link('INSERTAR NUEVO PRODUCTO', array('action' =>
                'insertapurpurina'), array('class' => 'buttonS bGreen'));
            break;
    }
    ?>
        <?php else: ?>

        <?php endif; ?>

    </div> 
</div> 

        <?php
//echo $this->Html->link('INSERTAR NUEVO TRABAJO', array('action' => 'nuevahojaruta'), array('class' => 'buttonS bGreen')); ?>